#include "StartLayer.h"
#include "DataManage.h"
#include "PlayLayer.h"
#include "ScoreBoard.h"
#include "SettingBoard.h"
#include "HelpBoard.h"
#include "AudioManage.h"

StartLayer* StartLayer::thisLayer;

StartLayer* StartLayer::getInstance()
{
	return thisLayer;
}

Scene* StartLayer::createScene()
{
	Scene* startScene = Scene::create();
	Layer* startLayer = StartLayer::create();

	startScene->addChild(startLayer);
	thisLayer = (StartLayer*)startLayer;

	return startScene;
}

bool StartLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}

	LayerColor* pBaseTemp = LayerColor::create(Color4B(252, 241, 142, 255));
	addChild(pBaseTemp);

	Size winSize = Director::getInstance()->getVisibleSize();

	Sprite* pRightCircle = Sprite::create("StartLayer/rightCircle.png");
	pRightCircle->setAnchorPoint(Point::ANCHOR_TOP_RIGHT);
	pRightCircle->setPosition(Point(winSize.width, winSize.height));
	pRightCircle->setScale(0.5f);
	addChild(pRightCircle);

	Sprite* pLeftCircle = Sprite::create("StartLayer/leftCircle.png");
	pLeftCircle->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
	pLeftCircle->setPosition(Point::ZERO);
	pLeftCircle->setScale(0.5f);
	addChild(pLeftCircle);

	Sprite* pTitleCircle = Sprite::create("StartLayer/title.png");
	pTitleCircle->setPosition(Point(winSize.width / 2, winSize.height * 0.72f));
	pTitleCircle->setScale(0.5f);
	addChild(pTitleCircle);

	// first Layer Button
	float flFirstButtonsOffsetY = winSize.height * 0.12f;
	float flFirstButtonsPosX = winSize.width * 0.65f;
	float flFirstButtonsPosY = winSize.height * 0.4f;

	m_pButtonStart = MenuItemImage::create("StartLayer/bigButtonGreen.png", "StartLayer/bigButtonGreen.png", CC_CALLBACK_0(StartLayer::onStartButtonClick, this));
	m_pButtonStart->setPosition(Point(flFirstButtonsPosX, flFirstButtonsPosY));
	m_pButtonStart->setScale(0.5f);

	Label* pStartLabel = Label::createWithTTF("Start", "fonts/arial.ttf", 50);
	pStartLabel->setTextColor(Color4B::WHITE);
	pStartLabel->enableBold();
	pStartLabel->setPosition(Point(m_pButtonStart->getContentSize().width / 2, m_pButtonStart->getContentSize().height / 2));
	m_pButtonStart->addChild(pStartLabel);
	m_pButtonStart->setCascadeOpacityEnabled(true);

	m_pButtonScore = MenuItemImage::create("StartLayer/bigButtonOrange.png", "StartLayer/bigButtonOrange.png", CC_CALLBACK_0(StartLayer::onScoreButtonClick, this));
	m_pButtonScore->setPosition(Point(flFirstButtonsPosX, flFirstButtonsPosY - flFirstButtonsOffsetY));
	m_pButtonScore->setScale(0.5f);

	Label* pHelpLabel = Label::createWithTTF("Score", "fonts/arial.ttf", 50);
	pHelpLabel->setTextColor(Color4B::WHITE);
	pHelpLabel->enableBold();
	pHelpLabel->setPosition(Point(m_pButtonScore->getContentSize().width / 2, m_pButtonScore->getContentSize().height / 2));
	m_pButtonScore->addChild(pHelpLabel);
	m_pButtonScore->setCascadeOpacityEnabled(true);

	m_pButtonExit = MenuItemImage::create("StartLayer/bigButtonGray.png", "StartLayer/bigButtonGray.png", CC_CALLBACK_0(StartLayer::onExitButtonClick, this));
	m_pButtonExit->setPosition(Point(flFirstButtonsPosX, flFirstButtonsPosY - flFirstButtonsOffsetY * 2));
	m_pButtonExit->setScale(0.5f);

	Label* pExitLabel = Label::createWithTTF("Exit", "fonts/arial.ttf", 50);
	pExitLabel->setTextColor(Color4B::WHITE);
	pExitLabel->enableBold();
	pExitLabel->setPosition(Point(m_pButtonExit->getContentSize().width / 2, m_pButtonExit->getContentSize().height / 2));
	m_pButtonExit->addChild(pExitLabel);
	m_pButtonExit->setCascadeOpacityEnabled(true);

	float flIconOffsetX = 100.0f;

	m_pIcon1 = Sprite::create("PlayerBoard/icon1.png");
	m_pIcon1->setPosition(Point(flFirstButtonsPosX - flIconOffsetX, flFirstButtonsPosY));
	m_pIcon1->setScale(0.5f);
	addChild(m_pIcon1);

	m_pIcon2 = Sprite::create("PlayerBoard/icon2.png");
	m_pIcon2->setPosition(Point(flFirstButtonsPosX - flIconOffsetX, flFirstButtonsPosY - flFirstButtonsOffsetY));
	m_pIcon2->setScale(0.5f);
	addChild(m_pIcon2);

	m_pIcon3 = Sprite::create("PlayerBoard/icon3.png");
	m_pIcon3->setPosition(Point(flFirstButtonsPosX - flIconOffsetX, flFirstButtonsPosY - flFirstButtonsOffsetY * 2));
	m_pIcon3->setScale(0.5f);
	addChild(m_pIcon3);

	m_pButtonSet = MenuItemImage::create("ForSaleBoard/button_set.png", "ForSaleBoard/button_set.png", CC_CALLBACK_0(StartLayer::onSetButtonClick, this));
	m_pButtonSet->setPosition(Point(winSize.width * 0.9f, winSize.height * 0.06f));
	m_pButtonSet->setScale(0.7f);

	// second Layer Button
	float flSecondButtonsOffsetY = winSize.height * 0.093f;
	float flSecondButtonsPosX = winSize.width * 0.65f;
	float flSecondButtonsPosY = winSize.height * 0.42f;

	m_pButton3Player = MenuItemImage::create("StartLayer/bigButtonGreen.png", "StartLayer/bigButtonGreen.png", CC_CALLBACK_0(StartLayer::onStartWithPlayerCountClick, this, 3));
	m_pButton3Player->setPosition(Point(flSecondButtonsPosX, flSecondButtonsPosY));
	m_pButton3Player->setVisible(false);
	m_pButton3Player->setOpacity(0);
	m_pButton3Player->setScale(0.5f);

	Label* p3PlayerLabel = Label::createWithTTF("3 Players", "fonts/arial.ttf", 40);
	p3PlayerLabel->setTextColor(Color4B::WHITE);
	p3PlayerLabel->enableBold();
	p3PlayerLabel->setPosition(Point(m_pButton3Player->getContentSize().width / 2, m_pButton3Player->getContentSize().height / 2 + 7));
	m_pButton3Player->addChild(p3PlayerLabel);
	m_pButton3Player->setCascadeOpacityEnabled(true);

	m_pButton4Player = MenuItemImage::create("StartLayer/bigButtonGreen.png", "StartLayer/bigButtonGreen.png", CC_CALLBACK_0(StartLayer::onStartWithPlayerCountClick, this, 4));
	m_pButton4Player->setPosition(Point(flSecondButtonsPosX, flSecondButtonsPosY - flSecondButtonsOffsetY));
	m_pButton4Player->setVisible(false);
	m_pButton4Player->setOpacity(0);
	m_pButton4Player->setScale(0.5f);

	Label* p4PlayerLabel = Label::createWithTTF("4 Players", "fonts/arial.ttf", 40);
	p4PlayerLabel->setTextColor(Color4B::WHITE);
	p4PlayerLabel->enableBold();
	p4PlayerLabel->setPosition(Point(m_pButton4Player->getContentSize().width / 2, m_pButton4Player->getContentSize().height / 2 + 7));
	m_pButton4Player->addChild(p4PlayerLabel);
	m_pButton4Player->setCascadeOpacityEnabled(true);

	m_pButton5Player = MenuItemImage::create("StartLayer/bigButtonGreen.png", "StartLayer/bigButtonGreen.png", CC_CALLBACK_0(StartLayer::onStartWithPlayerCountClick, this, 5));
	m_pButton5Player->setPosition(Point(flSecondButtonsPosX, flSecondButtonsPosY - flSecondButtonsOffsetY * 2));
	m_pButton5Player->setVisible(false);
	m_pButton5Player->setOpacity(0);
	m_pButton5Player->setScale(0.5f);

	Label* p5PlayerLabel = Label::createWithTTF("5 Players", "fonts/arial.ttf", 40);
	p5PlayerLabel->setTextColor(Color4B::WHITE);
	p5PlayerLabel->enableBold();
	p5PlayerLabel->setPosition(Point(m_pButton5Player->getContentSize().width / 2, m_pButton5Player->getContentSize().height / 2 + 7));
	m_pButton5Player->addChild(p5PlayerLabel);
	m_pButton5Player->setCascadeOpacityEnabled(true);

	m_pButtonBack = MenuItemImage::create("StartLayer/bigButtonGray.png", "StartLayer/bigButtonGray.png", CC_CALLBACK_0(StartLayer::onBackButtonClick, this));
	m_pButtonBack->setPosition(Point(flSecondButtonsPosX, flSecondButtonsPosY - flSecondButtonsOffsetY * 3));
	m_pButtonBack->setVisible(false);
	m_pButtonBack->setOpacity(0);
	m_pButtonBack->setScale(0.5f);

	Label* pBackLabel = Label::createWithTTF("Back", "fonts/arial.ttf", 40);
	pBackLabel->setTextColor(Color4B::WHITE);
	pBackLabel->enableBold();
	pBackLabel->setPosition(Point(m_pButtonBack->getContentSize().width / 2, m_pButtonBack->getContentSize().height / 2 + 7));
	m_pButtonBack->addChild(pBackLabel);
	m_pButtonBack->setCascadeOpacityEnabled(true);

	// Buttons Menu
	Menu* pButtonsMenu = Menu::create(m_pButtonStart, m_pButtonScore, m_pButtonExit, m_pButtonSet,
									  m_pButton3Player, m_pButton4Player, m_pButton5Player, m_pButtonBack, nullptr);
	pButtonsMenu->setPosition(Point::ZERO);
	addChild(pButtonsMenu);

	AudioManage::playStartMusic();

    // 提前加载卡牌素材
    for (int i = 1; i <= 30; i++)
    {
        __String* pImagePath = __String::createWithFormat("Card/card%02d.png", i);
        Director::getInstance()->getTextureCache()->addImageAsync(pImagePath->getCString(), nullptr);
    }
    
    for (int i = 0; i <= 15; i++)
    {
        __String* pImagePath = __String::createWithFormat("Card/money%02d.png", i);
        Director::getInstance()->getTextureCache()->addImageAsync(pImagePath->getCString(), nullptr);
    }
    
	return true;
}


void StartLayer::onStartButtonClick()
{
	AudioManage::button1Sound();

	if (m_bButtonLayer1InUse == false)
	{
		return;
	}
	
	m_bButtonLayer1InUse = false;

	ActionInterval* pHideAction = Sequence::create(FadeOut::create(0.5f), Hide::create(), nullptr);

	m_pButtonStart->runAction(pHideAction);
	m_pButtonScore->runAction(pHideAction->clone());
	m_pButtonExit->runAction(pHideAction->clone());

	m_pIcon1->runAction(pHideAction->clone());
	m_pIcon2->runAction(pHideAction->clone());
	m_pIcon3->runAction(pHideAction->clone());

	show2LayerButtons();
}

void StartLayer::onScoreButtonClick()
{
	AudioManage::button1Sound();

	ScoreBoard* pScoreBoard = ScoreBoard::create(ScoreBoard::HISTORY_SCORE);
	pScoreBoard->setPosition(0, 0);
	pScoreBoard->setScale(0.5f);
	addChild(pScoreBoard);
}

void StartLayer::onExitButtonClick()
{
    Director::getInstance()->end();
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void StartLayer::onSetButtonClick()
{
	AudioManage::buttonSound();

	SettingBoard* pSettingBoard = SettingBoard::create();
	pSettingBoard->setPosition(0, 0);
	pSettingBoard->setScale(0.5f);
	addChild(pSettingBoard);
}

void StartLayer::onStartWithPlayerCountClick(int nCount)
{
	DataManage::getInstance()->setPlayerCount(nCount);

	auto scene = PlayLayer::createScene();
	Director::getInstance()->replaceScene(scene);

	AudioManage::button1Sound();
	AudioManage::stopStartMusic();
}

void StartLayer::onBackButtonClick()
{
	if (m_bButtonLayer2InUse == false)
	{
		return;
	}

	AudioManage::button2Sound();

	m_bButtonLayer2InUse = false;

	ActionInterval* pHideAction = Sequence::create(FadeOut::create(0.5f), Hide::create(), nullptr);

	m_pButton3Player->runAction(pHideAction);
	m_pButton4Player->runAction(pHideAction->clone());
	m_pButton5Player->runAction(pHideAction->clone());
	m_pButtonBack->runAction(pHideAction->clone());

	show1LayerButtons();
}

void StartLayer::show2LayerButtons()
{
	MenuItem* p2LayerButtons[4] = {m_pButton3Player, m_pButton4Player, m_pButton5Player, m_pButtonBack};
	for (int i = 0; i < 4; i++)
	{
		auto funLayer2ButtonsCanUse = [this, i]()
		{
			if (i == 3)
			{
				m_bButtonLayer2InUse = true;
			}
		};

		p2LayerButtons[i]->setOpacity(0);
		p2LayerButtons[i]->setVisible(true);
		p2LayerButtons[i]->setPositionX(p2LayerButtons[i]->getPositionX() + 50);
		p2LayerButtons[i]->runAction(Sequence::create(DelayTime::create(0.3f + i * 0.2f),
													  Spawn::create(FadeIn::create(0.3f), MoveBy::create(0.3f, Point(-50, 0)), nullptr),
													  CallFunc::create(funLayer2ButtonsCanUse),
													  nullptr));
	}
}

void StartLayer::show1LayerButtons()
{
	MenuItem* p1LayerButtons[3] = {m_pButtonStart, m_pButtonScore, m_pButtonExit};
	for (int i = 0; i < 3; i++)
	{
		auto funLayer1ButtonsCanUse = [this, i]()
		{
			if (i == 2)
			{
				m_bButtonLayer1InUse = true;
			}
		};

		p1LayerButtons[i]->setOpacity(0);
		p1LayerButtons[i]->setVisible(true);
		p1LayerButtons[i]->setPositionX(p1LayerButtons[i]->getPositionX() + 50);
		p1LayerButtons[i]->runAction(Sequence::create(DelayTime::create(0.3f + i * 0.2f),
			Spawn::create(FadeIn::create(0.3f), MoveBy::create(0.3f, Point(-50, 0)), nullptr),
			CallFunc::create(funLayer1ButtonsCanUse),
			nullptr));
	}

	Sprite* pIcons[3] = {m_pIcon1, m_pIcon2, m_pIcon3};
	for (int i = 0; i < 3; i++)
	{
		pIcons[i]->setOpacity(0);
		pIcons[i]->setVisible(true);

		pIcons[i]->runAction(Sequence::create(DelayTime::create(0.3f + 0.1f * i), 
			Spawn::create(FadeIn::create(0.3f), MoveBy::create(0.1f, Point(0, 20.0f)), nullptr),
			MoveBy::create(0.1f, Point(0, -20)),
			nullptr));
	}
}
